/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jsibbold.zoomage.example;

import com.jsibbold.zoomage.ZoomageView;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.animator.PopupAnimator;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.core.CenterPopupView;
import com.lxj.xpopup.enums.PopupAnimation;
import com.lxj.xpopup.interfaces.SimpleCallback;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.AbsButton;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Switch;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.app.Context;

/**
 * 文件描述
 *
 * @since 2021-05-21
 */
public class CustomSwitchPopup extends CenterPopupView implements AbsButton.CheckedStateChangedListener,
    Component.ClickedListener {
    private static final int FROMARGBINT = 0xffffffff;
    private static final int SHOWSHADOWCOLOR = 0xFFD2D2D2;
    private static final int DISMISSSHADOWCOLOR = 0xff7f7f7f;
    private static final int SETCORNERRADIUS = 10;
    private static final int WIDTH = 130;
    private final ZoomageView zoomageView;
    private final AbilitySlice mAbilitySlice;
    private DirectionalLayout dlDirectional;
    private Switch zoomable;
    private Switch translatable;
    private Switch restrictBounds;
    private Switch animateOnReset;
    private Switch autoCenter;
    private ShadowLayout shadowLayout;

    /**
     * getIntFromAttr
     *
     * @param context 实体类
     * @param abilitySlice abilitySlice
     * @param zoomageView 实体类
     */
    public CustomSwitchPopup(Context context, AbilitySlice abilitySlice, ZoomageView zoomageView) {
        super(context, null);
        this.zoomageView = zoomageView;
        this.mAbilitySlice = abilitySlice;
    }

    public ShadowLayout getShadowLayout() {
        return shadowLayout;
    }

    // 返回自定义弹窗的布局
    @Override
    protected int getImplLayoutId() {
        return ResourceTable.Layout_custom_switch_popup;
    }

    // 执行初始化操作，比如：findComponentById，设置点击，或者任何你弹窗内的业务逻辑
    @Override
    protected void onCreate() {
        super.onCreate();
        initComponent();
        initData();
    }

    /**
     * 初始化组件
     */
    public void initComponent() {
        shadowLayout = (ShadowLayout) findComponentById(ResourceTable.Id_shadowLayout);
        dlDirectional = (DirectionalLayout) findComponentById(ResourceTable.Id_dl_directional);
        DirectionalLayout dlZoomable = (DirectionalLayout) findComponentById(ResourceTable.Id_dlZoomable);
        dlZoomable.setClickedListener(this);
        DirectionalLayout dlTranslatable = (DirectionalLayout) findComponentById(ResourceTable.Id_dlTranslatable);
        dlTranslatable.setClickedListener(this);
        DirectionalLayout dlRestrictBounds = (DirectionalLayout) findComponentById(ResourceTable.Id_dlRestrictBounds);
        dlRestrictBounds.setClickedListener(this);
        DirectionalLayout dlAnimateOnReset = (DirectionalLayout) findComponentById(ResourceTable.Id_dlAnimateOnReset);
        dlAnimateOnReset.setClickedListener(this);
        DirectionalLayout dlAutoCenter = (DirectionalLayout) findComponentById(ResourceTable.Id_dlAutoCenter);
        dlAutoCenter.setClickedListener(this);
        zoomable = (Switch) findComponentById(ResourceTable.Id_zoomable);
        translatable = (Switch) findComponentById(ResourceTable.Id_translatable);
        restrictBounds = (Switch) findComponentById(ResourceTable.Id_restrictBounds);
        animateOnReset = (Switch) findComponentById(ResourceTable.Id_animateOnReset);
        autoCenter = (Switch) findComponentById(ResourceTable.Id_autoCenter);
        Text close = (Text) findComponentById(ResourceTable.Id_close);
        Text autoResetMode = (Text) findComponentById(ResourceTable.Id_autoResetMode);
        Text reset = (Text) findComponentById(ResourceTable.Id_reset);
        close.setClickedListener(this);
        autoResetMode.setClickedListener(this);
        reset.setClickedListener(this);
        zoomable.setCheckedStateChangedListener(this);
        translatable.setCheckedStateChangedListener(this);
        restrictBounds.setCheckedStateChangedListener(this);
        animateOnReset.setCheckedStateChangedListener(this);
        autoCenter.setCheckedStateChangedListener(this);
    }

    /**
     * 设置数据
     */
    public void initData() {
        ShapeElement element = new ShapeElement();
        element.setRgbColor(RgbColor.fromArgbInt(FROMARGBINT));
        element.setCornerRadius(SETCORNERRADIUS);
        shadowLayout.setShadowRadius(SETCORNERRADIUS);
        dlDirectional.setBackground(element);
        zoomable.setChecked(zoomageView.isZoomEnabled());
        translatable.setChecked(zoomageView.isPanEnabled());
        restrictBounds.setChecked(zoomageView.isRestrictBounds());
        animateOnReset.setChecked(zoomageView.isAnimateOnReset());
        autoCenter.setChecked(zoomageView.getAutoCenter());
        shadowLayout.setWidth(mAbilitySlice.getWindow().getLayoutConfig().get().width - WIDTH);
    }

    // 设置自定义动画器，看需要而定
    @Override
    protected PopupAnimator getPopupAnimator() {
        return super.getPopupAnimator();
    }

    //    @Override
    protected int getMaxWidth() {
        return mAbilitySlice.getWindow().getLayoutConfig().get().width - WIDTH;
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_close:
                dismiss(); // 关闭弹窗
                break;
            case ResourceTable.Id_autoResetMode:
                new XPopup.Builder(getContext())
                    .popupAnimation(PopupAnimation.NoAnimation)
                    .setPopupCallback(new SimpleCallback() {
                        @Override
                        public void onShow(BasePopupView basePopupView) {
                            // 完全显示的时候执行
                            shadowLayout.setShadowColor(SHOWSHADOWCOLOR);
                        }

                        @Override
                        public void onDismiss(BasePopupView basePopupView) {
                            // 完全隐藏的时候执行
                            shadowLayout.setShadowColor(DISMISSSHADOWCOLOR);
                        }
                    })
                    .asCustom(new CustomResetModePopup(getContext(), mAbilitySlice, zoomageView))
                    .show();
                break;
            case ResourceTable.Id_reset:
                zoomageView.setReset();
                break;

            case ResourceTable.Id_dlZoomable:
                zoomable.setChecked(!zoomable.isChecked());
                break;
            case ResourceTable.Id_dlTranslatable:
                translatable.setChecked(!translatable.isChecked());
                break;
            case ResourceTable.Id_dlRestrictBounds:
                restrictBounds.setChecked(!restrictBounds.isChecked());
                break;
            case ResourceTable.Id_dlAnimateOnReset:
                animateOnReset.setChecked(!animateOnReset.isChecked());
                break;
            case ResourceTable.Id_dlAutoCenter:
                autoCenter.setChecked(!autoCenter.isChecked());
                break;
            default:
        }
    }

    @Override
    public void onCheckedChanged(AbsButton absButton, boolean isChecked) {
        switch (absButton.getId()) {
            case ResourceTable.Id_zoomable:
                zoomageView.setZoomEnabled(isChecked);
                break;
            case ResourceTable.Id_translatable:
                zoomageView.setPanEnabled(isChecked);
                break;
            case ResourceTable.Id_restrictBounds:
                zoomageView.setRestrictBounds(isChecked);
                break;
            case ResourceTable.Id_animateOnReset:
                zoomageView.setAnimateOnReset(isChecked);
                break;
            case ResourceTable.Id_autoCenter:
                zoomageView.setAutoCenter(isChecked);
                break;
            default:
        }
    }
}